package cn.wanda.bi.sunshine.platform.server.utils.objects;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Created by wangfupeng1.
 */
public final class If<T> {

    private boolean value;

    private If(Boolean value) {
        this.value = value;
    }

    public static <T> If<T> of(boolean value) {
        return new If(value);
    }

    public void isTrue(Action action) {
        if (value)
            action.doIt();
    }

    public <R> R isTrue(T t, Function<T, R> func) {
        if (value)
            return func.apply(t);
        return null;
    }

    public void isNotTrue(Action action) {
        if (!value)
            action.doIt();
    }

    public <R> R isNotTrue(T t, Function<T, R> func) {
        if (!value)
            return func.apply(t);
        return null;
    }

    public <X extends Throwable> T isTrueThrow(Supplier<? extends X> supplier) throws X {
        if (value)
            throw supplier.get();
        return null;
    }

    public <X extends Throwable> T isNotTrueThrow(Supplier<? extends X> supplier) throws X {
        if (!value)
            throw supplier.get();
        return null;
    }


}
